<?php

    define('ACC', true);
    require_once('../include/init.php');


    $getData = $_POST;
    $data = array();

    $goods = new goodModel();

    $data = $goods->shiftData($getData);  //过滤掉不是数据库字段的数据
    $num = $goods->autoNum();     //自动生成商品货号

    if($num){
        $data['goods_sn'] = $num;
    }

    $re = $goods->_validate($data);
    $err = $goods->getErr();

    if(!$re){
        echo implode(',',$err);
        exit;
    }


    //图片处理 判断是否修改了图片
    $file = new UpTool();
    $url = $file->up('ori_img');
//    if (!$url) {
//        print_r($file->error());
//    }

    if($url) {
        $data['ori_img'] = $url;
//    将缩略图加入数据库，如果原图上传成功，再次生成中等大小的缩略图， 300*400, 和浏览时用的缩略图，160*220
        if ($url) {
            $goodsdir = ROOT . substr($url, 0, strrpos($url, "/") + 1) . 'goods_' . substr($url, strrpos($url, "/") + 1);
            $thumbdir = ROOT . substr($url, 0, strrpos($url, "/") + 1) . 'thumb_' . substr($url, strrpos($url, "/") + 1);
        }
//    调用图片处理类生成缩略图
        if (ImageTool::thumb(ROOT . $url, 300, 400, $goodsdir)) {
            $data['goods_img'] = str_replace(ROOT, '', $goodsdir);
        }
        if (ImageTool::thumb(ROOT . $url, 160, 220, $thumbdir)) {
            $data['thumb_img'] = str_replace(ROOT, '', $thumbdir);
        }
    }



    if($goods->goodUpdate($data)){
        echo "商品更新成功！";
    }else{
        echo "商品更新失败！";
    }

//    header('./goodlist.php');
